﻿Imports System.Data.SqlClient

Public Class FormSubjectEditor

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Me.txtCODE.Enabled = True Then
            Dim cn As SqlConnection
            cn = New SqlConnection
            cn.ConnectionString = "server=.\SQLEXPRESS;uid=sa;pwd=123;database=SchoolDb;"
            cn.Open() 

            Dim cmd As SqlCommand
            cmd = New SqlCommand()
            cmd.Connection = cn 
            cmd.CommandText = "INSERT INTO TBL_SUBJECT VALUES(N'" & txtCODE.Text & "',N'" & txtNAME.Text & "',N'" & cboTYPE.Text & "'," & txtPRICE.Text & ")"
             
            cmd.ExecuteNonQuery()
            Me.Close()
            FormSubject.RefreshData()
        Else
            Dim cn As SqlConnection
            cn = New SqlConnection
            cn.ConnectionString = "server=.;uid=sa;pwd=123;database=SchoolDb;"
            cn.Open()

            Dim cmd As SqlCommand
            cmd = New SqlCommand()
            cmd.Connection = cn
            cmd.CommandText = "UPDATE TBL_SUBJECT SET SUBJECT_NAME=N'" & txtNAME.Text & "',TYPE=N'" & cboTYPE.Text & "',PRICE=" & txtPRICE.Text & " WHERE SUBJECT_CODE=N'" & txtCODE.Text & "';"

            cmd.ExecuteNonQuery()
            Me.Close()
            FormSubject.RefreshData()
        End If

        
         
    End Sub
End Class